<?php

namespace Modules\Coupon\Http\Controllers\API;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Coupon\Entities\Coupon;
use Illuminate\Support\Facades\Auth;
use Modules\Coupon\Entities\CouponActive;

class CouponController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
        // 获取未使用的优惠卷
        $group = Coupon::where('user_id', Auth::id())
            ->where('order_id', 0)
            ->with('active')
            ->get()
            ->groupBy('coupon_active_id');

        $result = [];
        foreach ($group as $key => $value) {
            $active = CouponActive::find($key);
            $active->quantity = count($value);
            array_push($result, $active);
        }

        return $result;
    }

}
